package com.yl.chainStore.entity.warehouse

import com.baomidou.mybatisplus.annotation.IdType
import com.baomidou.mybatisplus.annotation.TableField
import com.baomidou.mybatisplus.annotation.TableId
import com.baomidou.mybatisplus.annotation.TableName
import com.yl.chainStore.common.Constants
import com.yl.chainStore.common.enums.Status
import com.yl.common.annotation.NoArg
import java.math.BigDecimal
import java.math.RoundingMode

/**
 * <p>
 * 出库明细
 * </p>
 *
 * @author cxw
 * @since 2021-06-10
 */
@NoArg
@TableName("warehouse_out_order_detail")
data class WarehouseOutOrderDetail(

    /**
     * id
     */
    @TableId(value = "id", type = IdType.AUTO)
    var id: Long,

    /**
     * 出库订单id
     */
    @TableField("out_order_id")
    var outOrderId: Long = -1,

    @TableField("count_num")
    var countNum : Int?=1,

    /**
     * 物品id
     */
    @TableField("item_id")
    var itemId: Long,

    @TableField(exist = false)
    var itemName: String? = null,

    @TableField(exist = false)
    var nickname: String? = null,

    @TableField(exist = false)
    var unit: String? = null,

    @TableField(exist = false)
    var specification: String? = null,

    @TableField("price")
    var price:Long = 0,

    /**
     * 物品数量
     */
    @TableField("good_count")
    var goodCount: Int,

    /**
     * 备注
     */
    @TableField("remark")
    var remark: String? = null,

    /**
     * 数据有效性
     */
    @TableField("enabled")
    var enabled: Int = 1,

    /**
     * 状态
     */
    @TableField("status")
    var status: Status = Status.UNKNOWN,


    /**
     * 出库关联仓库内批次物品id
     */
    @TableField("out_warehouse_good_batch_id")
    var outWarehouseGoodBatchId: Long? = null,

    /**
     * 品牌
     */
    @TableField(exist = false)
    var brand:String?=null

){
    val showGoodName: String
        get() {
            if(nickname.isNullOrEmpty()){
                return itemName?:return ""
            }
            return nickname!!
        }

    /**
     * 实际要显示的实际总金额
     */
    @TableField(exist = false)
    var sumMoneyShow: BigDecimal?= null
        get() {
            if(field == null ){
                val sumMoney = price * goodCount
                return BigDecimal(sumMoney.div(Constants.OtherConfig.MONEY_TO_DB_DOUBLE)).setScale(2, RoundingMode.HALF_UP)
            }
            return field
        }
        set(value) {
            if(value != null){
                val sumMoney = (value.multiply(BigDecimal(Constants.OtherConfig.MONEY_TO_DB_DOUBLE))).toLong()
                price = sumMoney/goodCount
                field = value
            }
        }

    /**
     * 实际要显示的实际单价
     */
    @TableField(exist = false)
    var priceShow: BigDecimal?= null
        get() {
            if(field == null ){
                return BigDecimal(price.div(Constants.OtherConfig.MONEY_TO_DB_DOUBLE)).setScale(2, RoundingMode.HALF_UP)
            }
            return field
        }
        set(value) {
            if(value != null){
                price = (value.multiply(BigDecimal(Constants.OtherConfig.MONEY_TO_DB_DOUBLE))).toLong()
                field = value
            }
        }



}
